package kiwi.assist.dev.guieditor.command {
	import ghostcat.util.ReflectUtil;

	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;
	import kiwi.assist.dev.guieditor.event.GUIOperateEvent;
	import kiwi.assist.dev.guieditor.model.UiConfig;
	import kiwi.assist.dev.guieditor.model.UiDetailVO;
	import kiwi.assist.dev.reflect.FlashUiComponentReflectFactoryImpl;
	import kiwi.assist.dev.reflect.IReflectFactory;

	import org.robotlegs.mvcs.Command;

	import flash.display.DisplayObject;

	/**
	 * 构造一个ui的实例
	 * 并添加到画布上
	 * @author zhangming.luo
	 */
	public class AddNewUi2CanvasCommand extends Command {
		[Inject]
		public var event : GUIEditorEvent;
		[Inject]
		public var detail : UiDetailVO;
		private var uiConfig : UiConfig;

		override public function execute() : void {
			super.execute();
			uiConfig = event.data;
			var factory : IReflectFactory;
			if (uiConfig.factoryClz) {
				// 有工厂的
				factory = injector.instantiate(ReflectUtil.getClass(uiConfig.factoryClz));
			} else {
				// 走默认工厂
				factory = injector.instantiate(FlashUiComponentReflectFactoryImpl);
			}
			var dos : DisplayObject = DisplayObject(factory.newInstance(uiConfig.defaultConfig));
			if (dos.name == "noname") {
			} else {
				dos.name = uiConfig.comp + uiConfig.generateId(dos);
			}
			uiConfig.instanceMap[dos] = true;
			detail.uiList.push(dos);
			detail.configList[dos] = uiConfig;
			dispatch(new GUIOperateEvent(GUIOperateEvent.UI_ADD, dos));
		}
	}
}
